package com.nchu.commondevelopmentframework.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nchu.commondevelopmentframework.entity.User;
import com.nchu.commondevelopmentframework.query.UserQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.nchu.commondevelopmentframework.mapper.UserMapper;
import com.nchu.commondevelopmentframework.service.UserService;

/**
 * @className: UserServiceImpl
 * @description: 用户实体Service实现类
 * @author: Li Chuanwei
 * @date: 2024/01/30 19:50
 * @Company: Copyright [日期] by [作者或个人]
 **/
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService<User>{

    @Autowired
    private UserMapper userMapper;

    /**
     * 分页查询用户
     *
     * @param userQuery 分页查询对象
     * @return 分页查询结果
     */
    @Override
    public Page<User> selectPage(UserQuery userQuery) {
        // 创建Page对象，并设置当前页码和每页显示数量
        Page<User> page = new Page<>(userQuery.getCurrent(), userQuery.getSize());

        // 创建QueryWrapper对象，并设置查询条件
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(StringUtils.isNotEmpty(userQuery.getUsername()),"username", userQuery.getUsername());
        // 调用MyBatis Plus提供的分页查询方法
        IPage<User> usersPage = userMapper.selectPage(page,queryWrapper);

        // 返回对象中包含了查询结果和分页信息
        return (Page<User>) usersPage;
    }
}
